<?php
/**
 * Created by PhpStorm.
 * User: Giles
 * Date: 2017/7/27
 * Time: 15:29
 */
namespace Admin\Controller;

use Admin\ORG\PHPExcel;

class OrdernewController extends BaseController
{
    /**
     * 列表
     *
     */
    public function index()
    {

        $isSign = array(
            '0' => '未签收',
            '1' => '已签收',
        );
        $this->assign('isSign', $isSign);
        $this->assign('agent', $this->getAgent());
        $this->assign('isPay', $this->getIsPay());
        $this->assign('batch', $this->getBatch());
        $this->display();
    }


    /**
     * Ajax获取省份
     */
    public function getArea(){
        $id = I('get.id');
        $where['parent_id']= isset($id) && !empty($id) ? $id : 1;
        $area=D('ApiAreas')->where($where)->select();
        $data = array();
        foreach($area as $val) {
            $data[] = array(
                'id' => $val['area_id'],
                'value' => $val['area_name']
            );
        }
        $this->ajaxReturn($data);
    }

    public function edit()
    {
        $id = I('get.id');
        if (IS_POST) {
            $data = I('post.');
            $id = $data['id'];
            $data['isRewire'] = isset($data['isRewire']) ? 1 : 0;
            //$data['buyType'] = isset($data['buyType']) ? 2 : 1;
            $data['agent'] = $this->getAgent($data['agent']);
            $data['province'] = $this->getCity($data['province']);
            $data['city'] = $this->getCity($data['city']);
            $data['district'] = $this->getCity($data['district']);
            unset($data['id']);
            $res = D('ApiOrdersNew')->where(array('id' => $id))->save($data);
            if ($res === false) {
                $this->ajaxError('操作失败');
            } else {
                $this->ajaxSuccess('添加成功');
            }
        } else {
            $province=D('ApiAreas')->where(array('parent_id'=>1))->select();
            $provin = array();
            foreach($province as $val) {
                $provin[$val['area_id']] = $val['area_name'];
            }
            //var_dump($provin);
            $detile = D('ApiOrdersNew')->where(array('id' => $id))->find();
            $agent = $this->getAgent();
            $this->assign('agent', $agent);
            $this->assign('buyType', $this->getType());
            $this->assign('province', $this->getCitySelect($detile['province'], 1));
            $this->assign('city', $this->getCitySelect($detile['city'], 2));
            $this->assign('district', $this->getCitySelect($detile['district'], 3));
            $this->assign('agentId', array_search($detile['agent'], $agent));
            $this->assign('batch', $this->getBatch(true));
            $this->assign('detail', $detile);
            $this->assign('isPay', $this->getIsPay());
            $this->display();
        }
    }

    public function del()
    {
        $postId = I('post.id');
        D('ApiOrdersNew')->where(array('id' => $postId))->save(array('isOk' => 2));
        $this->ajaxSuccess('操作成功');
    }

    /**
     * 添加
     */
    public function add()
    {
        if (IS_POST) {
            $data = I('post.');

            $data['isRewire'] = isset($data['isRewire']) ? 1 : 0;
            //$data['buyType'] = isset($data['buyType']) ? 2 : 1;
            $data['agent'] = $this->getAgent($data['agent']);
            $data['province'] = $this->getCity($data['province']);
            $data['city'] = $this->getCity($data['city']);
            $data['district'] = $this->getCity($data['district']);
            $res = D('ApiOrdersNew')->add($data);
            if ($res === false) {
                $this->ajaxError('操作失败');
            } else {
                $this->ajaxSuccess('添加成功');
            }
        } else {
            $province=D('ApiAreas')->where(array('parent_id'=>1))->select();
            $provin = array();
            foreach($province as $val) {
                $provin[$val['area_id']] = $val['area_name'];
            }
            $this->assign('buyType', $this->getType());
            $this->assign('agent', $this->getAgent());
            $this->assign('isPay', $this->getIsPay());
            $this->assign('batch', $this->getBatch(true));
            $this->display();
        }
    }

    public function put()
    {
        $id = I('get.id');
        if (IS_POST) {
            $data = I('post.');
            $id = $data['id'];
            unset($data['id']);
            unset($data['expressLog']);
            $res = D('ApiOrdersNew')->where(array('id' => $id))->save($data);
            if ($res === false) {
                $this->ajaxError('操作失败');
            } else {
                $this->ajaxSuccess('添加成功');
            }
        } else {
            $express = array(
                '中通快递' => '中通快递',
                '邮政速递' => '邮政速递'
            );
            $detile = D('ApiOrdersNew')->where(array('id' => $id))->find();
            $this->assign('detail', $detile);
            $this->assign('express', $express);
            $this->display();
        }
    }

    /**
     * 导出订单
     */
    public function import()
    {
        if (IS_POST) {
            $data = I('post.');
            $where = array(
                'batch' => $data['batch']
            );
            if($data['agent'] != '999') {
                $where['agent'] = $this->getAgent($data['agent']);
            }

            import('Vendor.PHPExcel');
            import("Vendor.PHPExcel.Worksheet.Drawing");
            import("Vendor.PHPExcel.Writer.Excel2007");
            $objPHPExcel = new \PHPExcel();
            $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
            $objActSheet = $objPHPExcel->getActiveSheet();
            // 水平居中（位置很重要，建议在最初始位置）
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //表头
            $objActSheet->setCellValue('A1', '序号');
            $objActSheet->setCellValue('B1', '收件人');
            $objActSheet->setCellValue('C1', '收件电话');
            $objActSheet->setCellValue('D1', '收件省份');
            $objActSheet->setCellValue('E1', '收件城市');
            $objActSheet->setCellValue('F1', '收件区县');
            $objActSheet->setCellValue('G1', '收件人详细地址');
            $objActSheet->setCellValue('H1', '品类箱数');
            $objActSheet->setCellValue('I1', '快递单号');

            // 设置个表格宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(50);
            $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
            // 垂直居中
            $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);


            /**
            $res = D('ApiOrdersNew')->where($where)->group('userPhone')->select();
            $key = 2;
            foreach($res as $val) {
                $where['userPhone'] = $val['userPhone'];
                $onlyData = D('ApiOrdersNew')->where($where)->select();

                $jingpin = 0;
                $putong  = 0;
                foreach($onlyData as $only){
                    if($only['buyType'] == 1) {
                        $putong = $putong + $only['buyNumber'];
                    } else {
                        $jingpin = $jingpin + $only['buyNumber'];
                    }
                }
                $totle = $putong + $jingpin;
                if($totle <= 4) {
                    if((int)$jingpin == 0 && (int)$putong != 0) {
                        $buy = $putong.'箱普通';
                    }
                    if((int)$putong == 0 && (int)$jingpin != 0) {
                        $buy = $jingpin.'箱精品';
                    }
                    if((int)$putong != 0 && (int)$jingpin != 0){
                        $buy = $putong.'普'.$jingpin.'精';
                    }
                    $objActSheet->setCellValue('A'.$key , ($key-1));
                    $objActSheet->setCellValue('B'.$key, $val['userName']);
                    $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                    $objActSheet->setCellValue('D'.$key, $val['province']);
                    $objActSheet->setCellValue('E'.$key, $val['city']);
                    $objActSheet->setCellValue('F'.$key, $val['district']);
                    $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                    $objActSheet->setCellValue('H'.$key, $buy);
                    $key++;
                }
                if($totle <= 8 && $totle > 4) {
                    if((int)$putong >= 4) {
                        $buy = '4箱普通';
                        $objActSheet->setCellValue('A'.$key , ($key-1));
                        $objActSheet->setCellValue('B'.$key, $val['userName']);
                        $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                        $objActSheet->setCellValue('D'.$key, $val['province']);
                        $objActSheet->setCellValue('E'.$key, $val['city']);
                        $objActSheet->setCellValue('F'.$key, $val['district']);
                        $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                        $objActSheet->setCellValue('H'.$key, $buy);
                        $putong = $putong - 4;
                        $key++;
                    }
                    if((int)$jingpin >= 4) {
                        $buy = '4箱精品';
                        $objActSheet->setCellValue('A'.$key , ($key-1));
                        $objActSheet->setCellValue('B'.$key, $val['userName']);
                        $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                        $objActSheet->setCellValue('D'.$key, $val['province']);
                        $objActSheet->setCellValue('E'.$key, $val['city']);
                        $objActSheet->setCellValue('F'.$key, $val['district']);
                        $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                        $objActSheet->setCellValue('H'.$key, $buy);
                        $jingpin = $jingpin -4;
                        $key++;
                    }
                    if((int)$jingpin < 4 && (int)$putong < 4 && $jingpin != 0 && $putong != 0) {
                        $cellVa = 4 - (int)$putong;
                        $buy = $putong.'普'.$cellVa.'精';
                        $objActSheet->setCellValue('A'.$key , ($key-1));
                        $objActSheet->setCellValue('B'.$key, $val['userName']);
                        $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                        $objActSheet->setCellValue('D'.$key, $val['province']);
                        $objActSheet->setCellValue('E'.$key, $val['city']);
                        $objActSheet->setCellValue('F'.$key, $val['district']);
                        $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                        $objActSheet->setCellValue('H'.$key, $buy);
                        $jingpin = (int)$jingpin - (int)$cellVa;
                        $putong  = 0;
                        $key++;
                    }
                    if((int)$jingpin == 0 && (int)$putong != 0) {
                        $buy = $putong.'箱普通';
                    }
                    if((int)$putong == 0 && (int)$jingpin != 0) {
                        $buy = $jingpin.'箱精品';
                    }
                    if((int)$putong != 0 && (int)$jingpin != 0){
                        $buy = $putong.'普'.$jingpin.'精';
                    }
                    $objActSheet->setCellValue('A'.$key , ($key-1));
                    $objActSheet->setCellValue('B'.$key, $val['userName']);
                    $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                    $objActSheet->setCellValue('D'.$key, $val['province']);
                    $objActSheet->setCellValue('E'.$key, $val['city']);
                    $objActSheet->setCellValue('F'.$key, $val['district']);
                    $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                    $objActSheet->setCellValue('H'.$key, $buy);
                    $key++;
                } else {
                    //箱以上的后面在写
                }
                unset($jingpin);
                unset($putong);

            }
            $date = date("YmdH",time());
            $fileName = "{$date}.xls";
            $fileName = iconv("utf-8", "gb2312", $fileName);

            //header('Content-Type: application/vnd.ms-excel');
            //header("Content-Disposition: attachment;filename=\"$fileName\"");
            //header('Cache-Control: max-age=0');

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save($fileName); //文件通过浏览器下载

            //file_put_contents($fileName, $str);
            if(is_file($fileName)) {
                $url = 'http://' . $_SERVER['SERVER_NAME'] . '/index.php?m=Home&c=Down&a=index&file='. $fileName;
                $this->ajaxSuccess($url);
            } else {
                $this->ajaxSuccess($fileName);
            }
            **/
            //新版的导出
            $res = D('ApiOrdersNew')->where($where)->group('userPhone')->select();
            $key = 2;
            $putongTOT = 0;
            $jingpinTOT = 0;
            foreach($res as $val) {
                $where['userPhone'] = $val['userPhone'];
                $onlyData = D('ApiOrdersNew')->where($where)->select();

                $jingpin = 0;
                $putong = 0;
                foreach ($onlyData as $only) {
                    if ($only['buyType'] == 1) {
                        $putong = $putong + $only['buyNumber'];
                    } else {
                        $jingpin = $jingpin + $only['buyNumber'];
                    }
                }
                $putongTOT = $putongTOT + $putong;
                $jingpinTOT = $jingpinTOT + $jingpin;
                if($jingpin > 4 && $jingpin <=8) {
                    $objActSheet->setCellValue('A'.$key , ($key-1));
                    $objActSheet->setCellValue('B'.$key, $val['userName']);
                    $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                    $objActSheet->setCellValue('D'.$key, $val['province']);
                    $objActSheet->setCellValue('E'.$key, $val['city']);
                    $objActSheet->setCellValue('F'.$key, $val['district']);
                    $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                    $objActSheet->setCellValue('H'.$key, '4箱精品');
                    $objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getFill()->getStartColor()->setARGB('FFFF0000');
                    //$objPHPExcel->getActiveSheet()->getColumnDimension( 'H'.$key)->getFillColor()->setRGB('FF0000' );
                    $key++;
                    $jingpin = $jingpin - 4;
                }

                if($putong > 4 && $putong <=8) {
                    $objActSheet->setCellValue('A'.$key , ($key-1));
                    $objActSheet->setCellValue('B'.$key, $val['userName']);
                    $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                    $objActSheet->setCellValue('D'.$key, $val['province']);
                    $objActSheet->setCellValue('E'.$key, $val['city']);
                    $objActSheet->setCellValue('F'.$key, $val['district']);
                    $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                    $objActSheet->setCellValue('H'.$key, '4箱普通');
                    $key++;
                    $putong = $putong - 4;
                }

                if($jingpin <= 4 && $jingpin > 0) {
                    $objActSheet->setCellValue('A'.$key , ($key-1));
                    $objActSheet->setCellValue('B'.$key, $val['userName']);
                    $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                    $objActSheet->setCellValue('D'.$key, $val['province']);
                    $objActSheet->setCellValue('E'.$key, $val['city']);
                    $objActSheet->setCellValue('F'.$key, $val['district']);
                    $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                    $objActSheet->setCellValue('H'.$key, $jingpin.'箱精品');
                    $objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle('H'.$key)->getFill()->getStartColor()->setARGB('FFFF0000');
                    //$objPHPExcel->getSt
                    //$objPHPExcel->getActiveSheet()->getColumnDimension( 'H'.$key)->setRGB('FF0000' );
                    $key++;
                }
                if($putong <= 4 && $putong > 0) {
                    $objActSheet->setCellValue('A'.$key , ($key-1));
                    $objActSheet->setCellValue('B'.$key, $val['userName']);
                    $objActSheet->setCellValue('C'.$key, $val['userPhone']);
                    $objActSheet->setCellValue('D'.$key, $val['province']);
                    $objActSheet->setCellValue('E'.$key, $val['city']);
                    $objActSheet->setCellValue('F'.$key, $val['district']);
                    $objActSheet->setCellValue('G'.$key, $val['userAddress']);
                    $objActSheet->setCellValue('H'.$key, $putong.'箱普通');
                    $key++;
                }
                unset($jingpin);
                unset($putong);
            }
            $lastKey = $key+1;
            $objActSheet->setCellValue('A'.$lastKey , '');
            $objActSheet->setCellValue('B'.$lastKey,  '');
            $objActSheet->setCellValue('C'.$lastKey,  '');
            $objActSheet->setCellValue('D'.$lastKey,  '');
            $objActSheet->setCellValue('E'.$lastKey,  '');
            $objActSheet->setCellValue('F'.$lastKey,  '');
            $objActSheet->setCellValue('G'.$lastKey,  '');
            $objActSheet->setCellValue('H'.$lastKey, $putongTOT.'普 '.$jingpinTOT.'精');
            $date = date("YmdH",time());
            $fileName = "{$date}.xls";
            $fileName = iconv("utf-8", "gb2312", $fileName);

            //header('Content-Type: application/vnd.ms-excel');
            //header("Content-Disposition: attachment;filename=\"$fileName\"");
            //header('Cache-Control: max-age=0');

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save($fileName); //文件通过浏览器下载

            //file_put_contents($fileName, $str);
            if(is_file($fileName)) {
                $url = 'http://' . $_SERVER['SERVER_NAME'] . '/index.php?m=Home&c=Down&a=index&file='. $fileName;
                $this->ajaxSuccess($url);
            } else {
                $this->ajaxSuccess($fileName);
            }

        } else {
            $this->getBatch();
            $this->assign('agent', $this->getAgent());
            $this->assign('batch', $this->getBatch());
            $this->display();
        }
    }

    public function count()
    {
        if (IS_POST) {
            $data = I('post.');
            $where = array();
            if(isset($data['batch']) && !empty($data['batch'])) {
                $where['batch'] = $data['batch'];
            }
            if($data['agent'] != '999' && !empty($data['agent'])) {
                $where['agent'] = $this->getAgent($data['agent']);
            }


            $res = D('ApiOrdersNew')->where($where)->select();
            $numjing = 0;
            $xiaoguo = 0;
            $pricexiaoguo = 0;
            $chengbenxiaoguo = 0;
            $pricejing = 0;
            $chengbenjingpin = 0;
            $numputong = 0;
            $priceputong = 0;
            $chengbenputong = 0;
            $kuaidi = 0;
            foreach($res as $check) {
                if($check['buyType'] == 1) {
                    $numputong = $numputong + $check['buyNumber'];
                    $priceputong = $priceputong + $check['buyPrice'];
                    $chengbenputong = $chengbenputong + $check['buyNumber'] * 45;
                }
                if($check['buyType'] == 2){
                    $numjing = $numjing + $check['buyNumber'];
                    $pricejing = $pricejing + $check['buyPrice'];
                    $chengbenjingpin = $chengbenjingpin + $check['buyNumber'] * 58;
                }
                if($check['buyType'] == 3) {
                    $xiaoguo = $xiaoguo + $check['buyNumber'];
                    $pricexiaoguo = $pricexiaoguo + $check['buyPrice'];
                    $chengbenxiaoguo = $chengbenxiaoguo + $check['buyNumber'] * 30;
                }
                $kuaidi = $kuaidi + $check['expressPrice'];
            }
            $payguoyuan = ($numputong * 45) + ($numjing * 58) + ($xiaoguo * 30);
            $data = array(
                'numberP' => $numputong,
                'numberJ' => $numjing,
                'numberX' => $xiaoguo,
                'lirunP'  => $priceputong - $chengbenputong,
                'lirunJ'  => $pricejing - $chengbenjingpin,
                'lirunX'  => $pricexiaoguo - $chengbenxiaoguo,
                'lirunZ'  => ($priceputong - $chengbenputong) + ($pricejing - $chengbenjingpin) + ($pricexiaoguo - $chengbenxiaoguo),
                'pay'     => $payguoyuan + $kuaidi
            );

            $this->ajaxSuccess($data);

        } else {
            $this->getBatch();
            $this->assign('agent', $this->getAgent());
            $this->assign('batch', $this->getBatch());
            $this->display();
        }
    }

    public function ajaxGetIndex() {
        $postData = I('post.');
        $start = $postData['start'] ? $postData['start'] : 0;
        $limit = $postData['length'] ? $postData['length'] : 20;
        $draw = $postData['draw'];
        $where = array('isOk' => 1);
        $getInfo = I('get.');

        if (isset($getInfo['userName']) && !empty($getInfo['userName'])) {
            $where['userName'] = urldecode($getInfo['userName']);
        }
        if (isset($getInfo['userPhone']) && !empty($getInfo['userPhone'])) {
            $where['userPhone'] = $getInfo['userPhone'];
        }
        if (isset($getInfo['isPay']) && !empty($getInfo['isPay'])) {
            $where['isPay'] = $getInfo['isPay'];
        }
        if (isset($getInfo['batch']) && !empty($getInfo['batch'])) {
            $where['batch'] = $getInfo['batch'];
        }
        if (isset($getInfo['agent']) && !empty($getInfo['agent'])) {
            $where['agent'] = $getInfo['agent'];
        }
        if (isset($getInfo['isSign']) && !empty($getInfo['isSign'])) {
            if($getInfo['isSign'] == 9) {
                $where['expressNumber'] = '';
            } else {
                $where['isSign'] = $getInfo['isSign'];
            }

        }
        $total = D('ApiOrdersNew')->where($where)->count();
        $info = D('ApiOrdersNew')->where($where)->order('id desc')->limit($start, $limit)->select();
        //echo D('ApiOrders')->getLastSql();
        $data = array(
            'draw'            => $draw,
            'recordsTotal'    => $total,
            'recordsFiltered' => $total,
            'data'            => $info
        );
        $this->ajaxReturn($data, 'json');
    }


    private function getBatch($add = false)
    {
        $batch = D('ApiOrdersNew')->field('batch')->group('batch')->select();
        $return  = array();
        $addreturn     = array();
        $i = 1;
        foreach($batch as $item) {
            $return[$item['batch']] = '第'.$this->numToWord($item['batch']). '批';
            if($i < $item['batch']) {
                $i=  $item['batch'];
            }
        }
        if($add) {
            $addreturn[$i] = '第'.$this->numToWord($i). '批';
            if(!empty($batch)) {
                $addreturn[$i + 1] = '第'.$this->numToWord($i + 1). '批';
            }
            return $addreturn;
        }
        return$return;
    }

    private function getAgent($id = '')
    {
        if(empty($id)) {
            $agents = D('ApiAgent')->select();
            $return = array(
                999 => '全部代理',
            );
            foreach($agents as $agent) {
                $return[$agent['id']] = $agent['name'];
            }

            return $return;
        } else {
            $agents = D('ApiAgent')->where(array('id' => $id))->find();
            return $agents['name'];
        }

    }

    private function getIsPay()
    {
        return array(
            1 => '已付款',
            2 => '未付款',
            3 => '部分付',
            4 => '不需要'
        );
    }
    private function getType()
    {
        return array(
            1 => '普通装',
            2 => '精品装',
            3 => '小果装'
        );
    }

    private function getCity($id) {
        $city = D('ApiAreas')->where(array('area_id' => $id))->find();
        return $city['area_name'];
    }

    private function getCitySelect($name, $type, $return = false) {
        $city = D('ApiAreas')->where(array('area_name' => $name, 'area_type' => $type))->find();
        if($return) {
            return $city['area_id'];
        }
        return "<option value=". $city['area_id'] ." selected >". $city['area_name'] ."</option>";
    }

    private function numToWord($num)
    {
        $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
        $chiUni = array('','十', '百', '千', '万', '亿', '十', '百', '千');

        $chiStr = '';

        $num_str = (string)$num;

        $count = strlen($num_str);
        $last_flag = true; //上一个 是否为0
        $zero_flag = true; //是否第一个
        $temp_num = null; //临时数字

        $chiStr = '';//拼接结果
        if ($count == 2) {//两位数
            $temp_num = $num_str[0];
            $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
            $temp_num = $num_str[1];
            $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
        }else if($count > 2){
            $index = 0;
            for ($i=$count-1; $i >= 0 ; $i--) {
                $temp_num = $num_str[$i];
                if ($temp_num == 0) {
                    if (!$zero_flag && !$last_flag ) {
                        $chiStr = $chiNum[$temp_num]. $chiStr;
                        $last_flag = true;
                    }
                }else{
                    $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;

                    $zero_flag = false;
                    $last_flag = false;
                }
                $index ++;
            }
        }else{
            $chiStr = $chiNum[$num_str[0]];
        }
        return $chiStr;
    }
}